<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form ref="filterForm" :model="filterForm" :inline="true" label-position="left" label-width="100px">
        <el-form-item label="关键" prop="keyword">
          <el-input v-model="filterForm.keyword" clearable placeholder="合约" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
        </el-form-item>
      </el-form>
    </div>
    
    <el-radio-group v-model="filterForm.workStatus"  @input="handleFilter">
      <el-radio-button label="0">全部</el-radio-button>
      <el-radio-button label="2">正常</el-radio-button>
      <el-radio-button label="1">禁用</el-radio-button>
    </el-radio-group>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      stripe
      border
      fit
      highlight-current-row
      :height="windowHeight"
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column label="任务编码" align="left" fixed="left" width="240px">
          <template slot-scope="{row}">
            <span>{{ row.workCode }}</span>
          </template>
      </el-table-column>
      <el-table-column label="任务名称" align="left" width="300px">
        <template slot-scope="{row}">
          <span>{{ row.workName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新" width="180px">
        <template slot-scope="{row}">
          <span>{{ row.prevTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新" width="180px">
        <template slot-scope="{row}">
          <span>{{ row.nextTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="间隔" width="100px">
        <template slot-scope="{row}">
          <span>{{ row.workInterval }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新" width="180px">
        <template slot-scope="{row}">
          <span>{{ row.updatedAt }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" min-width="150px">
        <template slot-scope="{row}">
          <span v-if="row.workStatus == 1" style="color:#f30;">禁用</span>
          <span v-else>正常</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="100" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button size="mini" v-if="row.workStatus == 1"  @click="onMissionOpenSubmit(row)">
            开启
          </el-button>
          <el-button size="mini" v-else  @click="onMissionCloseSubmit(row)">
            关闭
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="filterForm.start" :limit.sync="filterForm.perpage" @pagination="getList" />
  </div>
</template>

<script>
//import { getFaultinessList,  removeFaultinessByID, changeFaultinessStatusByID } from '@/api/development/faultiness'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import {getHost} from '@/utils/socket' // secondary package based on el-pagination

export default {
  name: 'financeDashboard',
  components: { Pagination },
  directives: { waves },
  filters: {
  },
  data() {
    return {
      
      socket:"",
      tableKey: 0,
      list: null,
      windowHeight:"800",
      total: 0,
      listLoading: false,
      connected:"loading",
      filterForm: {
        start: 1,
        perpage: 50,
        workStatus:'2',
        keyword: undefined
      }
    }
  },
  created() {
    //this.init()
  },
  destroyed () {
      // 销毁监听
      this.close()
  },
  methods: {
    init: function () {
      if(typeof(WebSocket) === "undefined"){
          alert("您的浏览器不支持socket")
      }else{
          // 实例化socket
          this.socket = new WebSocket(getHost())
          // 监听socket连接
          this.socket.onopen = this.open
          // 监听socket错误信息
          this.socket.onerror = this.error
          // 监听socket消息
          this.socket.onmessage = this.getMessage
      }
    },
    open: function () {

        this.connected = 'loaded'

        this.getList()
    },
    error: function (e) {
        console.log("连接错误",JSON.stringify(e))
    },
    getMessage: function (receiveData) {
      receiveData = JSON.parse(receiveData.data)
        console.log(receiveData)
      if(receiveData.status != 200){
          console.log(receiveData.msg)
      }else{
        if(receiveData.conversationType == "foundation.mission.list"){
            this.list = receiveData.data.list
            this.total = receiveData.data.total
        }else{
            this.getList()
        }
      }
        console.log(this.list)
    },
    send: function (params) {
      params = JSON.stringify(params)
      this.socket.send(params)
    },
    close: function () {
        console.log("socket已经关闭")
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    getList:function(){
      this.send({
        "conversationType":"foundation.mission.list",
        "message":{"page":this.filterForm.start,"size":this.filterForm.perpage,"workStatus":this.filterForm.workStatus},
        "target":{"id":"3"}
      })
    },
    onMissionCloseSubmit:function(order){
      let _this = this;
      let _formData = {
        "workCode":order.workCode,
        "workStatus":1
      }
      _this.send({
        "conversationType":"foundation.mission.close",
        "message":_formData,
        "target":{"id":"3"}
      })
    },
    onMissionOpenSubmit:function(order){
      let _this = this;
      let _formData = {
        "workCode":order.workCode,
        "workStatus":2
      }
      _this.send({
        "conversationType":"foundation.mission.open",
        "message":_formData,
        "target":{"id":"3"}
      })
    },
    handleFilter() {
      this.filterForm.page = 1
      this.getList()
    },
    sortChange:function(){

    }
  }
}
</script>
<style>

</style>